package com.kb.design.controller;

import com.kb.design.common.ApiResponse;
import com.kb.design.entity.BonusConfig;
import com.kb.design.entity.DesignDept;
import com.kb.design.service.DeptService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;;
import java.util.ArrayList;

/**
 * 部门信息
 * 
 * @author czx
 */
@RestController
@RequestMapping("/dept")
public class DeptController{
    @Resource
    private DeptService deptService;
    @PostMapping("/list")
    public ApiResponse list(@RequestBody DesignDept dept)
    {
        return ApiResponse.success(deptService.selectDeptList(dept));
    }
    /**
     * 新增保存部门
     */

    @PostMapping("/add")
    public ApiResponse addSave(@RequestBody @Validated DesignDept dept)
    {
//        if (!deptService.checkDeptNameUnique(dept))
//        {
//            return error("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
//        }
        deptService.insertDept(dept);
        return ApiResponse.success("新增成功");
    }
    @GetMapping("/remove")
    public ApiResponse remove(Long deptId)
    {
        if (deptService.selectDeptCount(deptId) > 0)
        {
            return ApiResponse.error("存在下级部门,不允许删除");
        }
        if (deptService.checkDeptExistUser(deptId))
        {
            return ApiResponse.error("部门存在用户,不允许删除");
        }
        //检查是否有权限
//        deptService.checkDeptDataScope(deptId);
        deptService.deleteDeptById(deptId);
        return  ApiResponse.success(new ArrayList<>());
    }
    /**
     * 修改保存部门
     */
    @PostMapping("/updateDept")
    public ApiResponse editSave(@RequestBody DesignDept dept)
    {
        Long deptId = dept.getDeptId();
//        deptService.checkDeptDataScope(deptId);
        if (!deptService.checkDeptNameUnique(dept))
        {
            return ApiResponse.error("修改部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        else if (dept.getParentId().equals(deptId))
        {
            return ApiResponse.error("修改部门'" + dept.getDeptName() + "'失败，上级部门不能是自己");
        }
//        else if (Objects.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0)
//        {
//            return ApiResponse.error("该部门包含未停用的子部门！");
//        }
        return ApiResponse.success(deptService.updateDept(dept));
    }
    /**
     * 查询区域
     *
     * @param
     * @return
     */
    @PostMapping("/queryArea")
    public ApiResponse queryArea() {
        return ApiResponse.success(deptService.queryArea());
    }

    /**
     * 根据参数查询部门
     *
     * @param
     * @return
     */
    @PostMapping("/getDeptById")
    public ApiResponse getDeptById(@RequestBody DesignDept dept) {
        return ApiResponse.success(deptService.getDeptById(dept));
    }
    /**
     * 查询二级部门
     *
     * @param
     * @return
     */
    @PostMapping("/querySecondaryDept")
    public ApiResponse querySecondaryDept() {
        return ApiResponse.success(deptService.querySecondaryDept());
    }
    /**
     * 职能部门筛选
     *
     * @param
     * @return
     */
    @PostMapping("/queryDepartmentScreening")
    public ApiResponse queryDepartmentScreening(@RequestBody DesignDept dept) {
        return ApiResponse.success(deptService.queryDepartmentScreening(dept));
    }

    /**
     * 日报部门下拉值
     *
     * @param
     * @return
     */
    @PostMapping("/queryDailyDepartment")
    public ApiResponse queryDailyDepartment(@RequestBody DesignDept dept) {
        return ApiResponse.success(deptService.queryDailyDepartment(dept));
    }
}
